import org.apache.commons.io.FileUtils;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.configuration.Configuration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangzj
 * @description 分布式缓存
 * @date 2020/7/16 0:07
 */
public class DistributedCache {
    public static void main(String[] args) throws Exception {
        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        //1：注册一个文件,可以使用hdfs上的文件 也可以是本地文件进行测试
        env.registerCachedFile("D:/MyOperation\\flinkdemo\\testfile\\test", "distributedCache");
        //2、创建dataSource的数据源
        DataSource<String> data = env.fromElements("linea", "lineb", "linec", "lined");

        DataSet<String> result = data.map(new RichMapFunction<String, String>() {
            private ArrayList<String> dataList = new ArrayList<>();

            /**
             * open打开了缓存的数据
             * @param parameters
             * @throws Exception
             */
            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                //3、使用缓存文件
                File myFile = getRuntimeContext().getDistributedCache().getFile("distributedCache");
                List<String> lines = FileUtils.readLines(myFile);
                for (String line : lines) {
                    this.dataList.add(line);
                    System.err.println("分布式缓存为:" + line);
                }
            }

            @Override
            public String map(String value) throws Exception {
                System.out.println("使用了dataList：" + dataList + "--------" + value);
                return dataList + ":" + value;
            }
        });
        result.printToErr();
    }
}
